﻿// UiTpTimer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "inc/CTimer.h"

DWORD g_startTime = GetTickCount();
#define WM_TIME1 (WM_USER + 90)
#define WM_TIME2 (WM_USER + 30)
class CUITimerCb : public ITimerCallback
{
public:
    void OnTimer(ITimer* lpTimer, TIMERINFO* lpTimerInfo)
    {
        wchar_t szBuf[120] = { 0 };
        wsprintf(szBuf, L"tid:%d hwnd:0x%x Msg:0x%x Time:%d(%d) cout:%d ExcTime:%d \n", GetCurrentThreadId(),
            lpTimerInfo->hWnd, lpTimerInfo->uTimerID, GetTickCount(), GetTickCount() - g_startTime, lpTimerInfo->uExcCnt, GetTickCount64() - lpTimerInfo->dwCreateTm);
        DWORD dwTid = 0;
        switch (lpTimerInfo->uTimerID)
        {
        case WM_TIME1:
            dwTid = GetCurrentThreadId();
            wprintf(szBuf);
            break;
        case WM_TIME2:
            dwTid = GetCurrentThreadId();
            wprintf(szBuf);
            Sleep(1000);
            if (lpTimerInfo->uExcCnt == 10)
            {
                lpTimer->KillTimer(lpTimerInfo->hWnd, WM_TIME2);
            }
            break;
        }
    }
};

VOID CALLBACK Timer_CallBack(ITimer* lpTimer, TIMERINFO_PTR lpTimerInfo)
{
    wchar_t szBuf[120] = { 0 };
    wsprintf(szBuf, L"Timer_CallBack Functor Msg:0x%x HWND:0x%x tid:%d Time:%d(%d) cout:%d ExcTime:%d context:0x%x\n", lpTimerInfo->uTimerID, lpTimerInfo->hWnd,
        GetCurrentThreadId(),
        GetTickCount(), GetTickCount() - g_startTime, lpTimerInfo->uExcCnt, GetTickCount64() - lpTimerInfo->dwCreateTm, lpTimerInfo->pvContext);
    wprintf(szBuf);

    if (lpTimerInfo->uExcCnt > 8)
    {
        lpTimer->KillTimer(lpTimerInfo);
    }
}
int main()
{
    if(1)
    {
        printf("Begin test UI Timer, Current Main tid:%d\n", GetCurrentThreadId());
        CUITimer UiTimer;//UI线程Timer 需要有消息循环才行

        CUITimerCb timerCb;
        UiTimer.SetTimer((HWND)0x1234, WM_TIME1, 1500, &timerCb);
        UiTimer.SetTimer((HWND)0x567, WM_TIME2, 1500, &timerCb);
        
        MessageBox(NULL, L"建立了Ui线程的消息循环了", L"Ui线程", NULL);// MessageBox会建立消息循环

        UiTimer.KillTimer((HWND)0x1234, WM_TIME1); // 通过句柄和ID匹配关闭timer
        UiTimer.KillTimer(WM_TIME1); // 通过消息ID匹配关闭timer
        UiTimer.KillTimer((HWND)0x567);  // 通过句柄匹配关闭timer
        std::cout << "Have killed two timer, and will set new timer!\n";

        UiTimer.SetTimer(1000, Timer_CallBack);

        MessageBox(NULL, L"已经关闭了两个UI Timer", L"Ui线程", NULL);
    }

    printf("Begin test TP Timer, Current Main tid:%d\n", GetCurrentThreadId());
    CTPTimer TpTimer; //Thread pool线程Timer 

    CUITimerCb timerCb;
    TpTimer.SetTimer((HWND)0x34567, WM_TIME1, 1200, &timerCb);
    TpTimer.SetTimer((HWND)0x5678,  WM_TIME2, 500,  &timerCb);

    TpTimer.SetTimer((HWND)0x634, 0x9999, 1000, &Timer_CallBack);

    getchar();

    TpTimer.KillTimer((HWND)0x34567, WM_TIME1);//通过消息ID和HWND匹配关闭
    TpTimer.KillTimer(WM_TIME2);//通过消息ID匹配关闭
    TpTimer.KillTimer((HWND)0x634);//通过HWND匹配关闭

    std::cout << "Have killed all timer!\n";
    getchar();
}

